package org.example.service.impl;

import com.alibaba.fastjson.JSON;
import org.example.domain.AGoodsMaterial;
import org.example.domain.Driver;
import org.example.mapper.AGoodsMaterialMapper;
import org.example.result.Transport;
import org.example.service.DriverService;
import org.example.mapper.DriverMapper;
import org.example.utils.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author 14168
* @description 针对表【sys_driver】的数据库操作Service实现
* @createDate 2024-06-03 09:58:24
*/
@Service
public class DriverServiceImpl implements DriverService{

    @Autowired
    private DriverMapper driverMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AGoodsMaterialMapper aGoodsMaterialMapper;
    @Override
    public List<Driver> list() {
        return driverMapper.list();
    }
    @Override
    public void updateStatus(Integer id) {
        driverMapper.updateStatus(id);
        Driver dbDriver = driverMapper.getById(id);
        List<Transport> list = redisTemplate.opsForList().range("driver:" + dbDriver.getUid(),0,-1);
        for (Transport driver : list) {
            AGoodsMaterial aGoodsMaterial = new AGoodsMaterial();
            SnowFlake snowFlake = new SnowFlake();
            aGoodsMaterial.setId(snowFlake.nextId()+"");
            aGoodsMaterial.setBusinessId(driver.getBusinessId());
            aGoodsMaterial.setMaterialHouseId(driver.getWarehouseId());
            aGoodsMaterial.setCropId(driver.getCropId()+"");
            aGoodsMaterial.setWorkTime(LocalDateTime.now());
            aGoodsMaterial.setWarehousingNum(driver.getNum());
            aGoodsMaterialMapper.add(aGoodsMaterial);
        }
    }
}




